package com.javabi.htmlbuilder.html.element.form;

import com.javabi.htmlbuilder.html.HTMLBuilder;
import com.javabi.htmlbuilder.html.HTMLContent;
import com.javabi.htmlbuilder.html.HTMLElementList;
import com.javabi.htmlbuilder.html.HTMLElementName;

/**
 * An HTML Form Element.
 * <p>
 * &lt;form&gt; ... &lt;/form&gt;
 * </p>
 */
public class Form extends HTMLElementList<Form> {

	/** The name. */
	private String name = null;
	/** The action. */
	private String action = null;
	/** The method. */
	private Method method = null;

	/**
	 * Creates a new form.
	 */
	public Form() {
	}

	/**
	 * Creates a new form.
	 * @param name the name.
	 */
	public Form(String name) {
		this.name = name;
	}

	/**
	 * Creates a new form.
	 * @param name the name.
	 * @param method the method.
	 */
	public Form(String name, Method method) {
		this.name = name;
		this.method = method;
	}

	/**
	 * Returns the name
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Returns the action
	 * @return the action
	 */
	public String getAction() {
		return action;
	}

	/**
	 * Sets the action
	 * @param action the action to set
	 */
	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * Returns the method
	 * @return the method
	 */
	public Method getMethod() {
		return method;
	}

	/**
	 * Sets the method
	 * @param method the method to set
	 */
	public void setMethod(Method method) {
		this.method = method;
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public HTMLElementName getElementName() {
		return HTMLElementName.FORM;
	}

	/**
	 * Append this to the given html builder.
	 * @param html the html builder.
	 * @return the html builder.
	 */
	public HTMLBuilder appendAttributesTo(HTMLBuilder html) {
		if (method != null) {
			html.attribute("method", method);
		}
		if (name != null) {
			html.attribute("name", name);
		}
		if (action != null) {
			html.attribute("action", action);
		}
		return html;
	}

	/**
	 * An HTTP Method.
	 */
	public static enum Method implements HTMLContent {

		/** The GET method. */
		GET,
		/** The POST method. */
		POST;

		/**
		 * Returns true to format this element.
		 * @return true to format this element.
		 */
		public boolean format() {
			return false;
		}

		/**
		 * Append this to the given html builder.
		 * @param html the html builder.
		 * @return the html builder.
		 */
		public HTMLBuilder appendTo(HTMLBuilder html) {
			html.content(name());
			return html;
		}
	}
}
